package com.gnerv.battle.boot.module.base.business.impl;

import com.gnerv.battle.boot.common.base.model.RestResult;
import com.gnerv.battle.boot.common.tools.UUIDTools;
import com.gnerv.battle.boot.module.base.business.NoticeBusiness;
import com.gnerv.battle.boot.module.base.model.bo.NoticeBO;
import com.gnerv.battle.boot.module.base.model.dto.NoticeCreateDTO;
import com.gnerv.battle.boot.module.base.model.dto.NoticeDTO;
import com.gnerv.battle.boot.module.base.model.dto.NoticeReceiveDTO;
import com.gnerv.battle.boot.module.base.model.dto.NoticeUpdateDTO;
import com.gnerv.battle.boot.module.base.service.CurrentAccountService;
import com.gnerv.battle.boot.module.base.service.NoticeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.gnerv.battle.boot.common.tools.PageTools.limitSize;
import static com.gnerv.battle.boot.common.tools.PageTools.limitStart;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class NoticeBusinessImpl implements NoticeBusiness {

    private final NoticeService noticeService;
    private final CurrentAccountService currentAccountService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(NoticeCreateDTO noticeCreateDTO) {
        NoticeBO noticeBO = noticeCreateDTO.toNoticeBO();
        noticeBO.setUkId(UUIDTools.lower());
        noticeService.create(noticeBO);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        noticeService.deleteByUkId(ukId);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(NoticeUpdateDTO noticeUpdateDTO) {
        noticeService.updateByUkId(noticeUpdateDTO.toNoticeBO());
        return RestResult.ok();
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<NoticeDTO> selectByUkId(String ukId) {
        return RestResult.ok(noticeService.selectByUkId(ukId));
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<List<NoticeDTO>> listByTitle(String title, Integer page, Integer size) {
        List<NoticeDTO> noticeDTOList = noticeService.listByTitle(title, limitStart(page, size), limitSize(size));
        int total = noticeService.countByTitle(title);
        return RestResult.ok(noticeDTOList).page(page, size, total);
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<List<NoticeDTO>> listByAccountUkId(String accountUkId, Integer page, Integer size) {
        List<NoticeDTO> noticeDTOList = noticeService.listByAccountUkId(accountUkId, limitStart(page, size), limitSize(size));
        int total = noticeService.countByAccountUkId(accountUkId);
        return RestResult.ok(noticeDTOList).page(page, size, total);
    }

    @Override
    public RestResult<Void> send(NoticeReceiveDTO noticeReceiveDTO) {
        noticeService.send(noticeReceiveDTO.getNoticeUkId(), noticeReceiveDTO.getAccountUkIdList());
        return RestResult.ok();
    }

    @Override
    public RestResult<NoticeDTO> selectReceiveByNoticeUkId(String noticeUkId) {
        String accountUkId = currentAccountService.accountUkId();
        NoticeDTO noticeDTO = noticeService.selectReceiveByAccountUkIdAndNoticeUkId(accountUkId, noticeUkId);
        return RestResult.ok(noticeDTO);
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<List<NoticeDTO>> listReceive(String status, Integer page, Integer size) {
        String accountUkId = currentAccountService.accountUkId();
        List<NoticeDTO> noticeDTOList = noticeService.listReceive(accountUkId, status, limitStart(page, size), limitSize(size));
        int total = noticeService.countReceive(accountUkId, status);
        return RestResult.ok(noticeDTOList).page(page, size, total);
    }

}
